import mongoose from "mongoose";
import dayjs from "dayjs";

const Schema = mongoose.Schema;

const BookInstanceSchema = new Schema({
  book: {
    type: Schema.Types.ObjectId,
    ref: "Book",
    required: true,
  }, // 相关藏书的引用
  imprint: { type: String, required: true }, // 出版项
  status: {
    type: String,
    required: true,
    enum: ["Available", "Maintenance", "Loaned", "Reserved"],
    default: "Maintenance",
  },
  due_back: { type: Date, default: Date.now },
});

BookInstanceSchema.virtual("url").get(function () {
  return "/catalog/bookinstance/" + this._id;
});

BookInstanceSchema.virtual("due_back_formatted").get(function () {
  return dayjs(this.due_back).locale("zh-cn").format("YYYY-MM-DD");
});

export default mongoose.model("BookInstance", BookInstanceSchema);
